home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Online / opennap / setup.c < prev    next >
C/C++ Source or Header  |  2001-06-08  |  6KB  |  242 lines

  1. /* Copyright (C) 2000-1 drscholl@users.sourceforge.net
  2.    This is free software distributed under the terms of the
  3.    GNU Public License.  See the file COPYING for details.
  4.  
  5.    $Id: setup.c,v 1.5 2001/02/15 08:39:45 drscholl Exp $ */
  6.  
  7. /* Modified 29/05/01 : Moved #include <sys/types.h> above #include
  8.       for Amiga port : <sys/stat.h> so that the types used in sys/types.h are
  9.                        typedef'd beforehand
  10.  
  11.                        Added include "confdefs.h" since we are not using the
  12.                        CONFIGURE script
  13.  
  14.                        Removed default SHAREDIR, since we can't really assume
  15.                        a default directory on the Amiga
  16. */
  17.  
  18. #include "confdefs.h"
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <errno.h>
  23.  
  24. #include <sys/types.h>
  25.  
  26. #include <sys/stat.h>
  27. /*#include <sys/types.h>*/
  28. #include <fcntl.h>
  29. #ifndef WIN32
  30. #include <unistd.h>
  31. #define MKDIR(a,b) mkdir(a,b)
  32. #define DIRSEP '/'
  33. #else
  34. #include <windows.h>
  35. #include <direct.h>
  36. #include <io.h>
  37. #define MKDIR(a,b) mkdir(a)
  38. #define F_OK 00
  39. #define DIRSEP '\\'
  40. #define S_IRUSR _S_IREAD
  41. #define S_IWUSR _S_IWRITE
  42. #endif
  43.  
  44. /* opennap installation program */
  45.  
  46. #if WIN32
  47. #define EXT ".exe"
  48. #include "opennap.h"
  49. #else
  50. #define EXT ""
  51. #endif /* WIN32 */
  52.  
  53. static void
  54. usage (void)
  55. {
  56.     printf ("usage: setup%s [ -h ] [ -v ]\n", EXT);
  57.     puts ("  -h display this help message");
  58.     puts ("  -v display the version");
  59.     exit (0);
  60. }
  61.  
  62. static void
  63. version (void)
  64. {
  65.     printf ("%s setup%s %s", PACKAGE, EXT, VERSION);
  66.     exit (0);
  67. }
  68.  
  69. static void
  70. prompt (const char *str, const char *def, char *buf, int buflen)
  71. {
  72.     char   *p;
  73.  
  74.     buf[buflen - 1] = 0;
  75.     while (1)
  76.     {
  77.         printf ("%s? [%s]: ", str, def ? def : "<no default>");
  78.         fflush (stdout);
  79.         if (!fgets (buf, buflen - 1, stdin))
  80.         {
  81.             puts ("EOF");
  82.             exit (1);
  83.         }
  84.         if (buf[0] == '\n' || buf[0] == '\r')
  85.         {
  86.             if (def)
  87.             {
  88.                 strncpy (buf, def, buflen - 1);
  89.                 return;
  90.             }
  91.         }
  92.         else
  93.         {
  94.             p = strpbrk (buf, " \r\n");
  95.             *p = 0;
  96.             if (buf[0])
  97.             {
  98.                 return;
  99.             }
  100.         }
  101.     }
  102. }
  103.  
  104. int
  105. main (int argc, char **argv)
  106. {
  107.     int     i;
  108.     int     fd;
  109.     char    path[256];
  110.     char    buf[256];
  111.     char    nick[64];
  112.     char    pass[64];
  113.     char    email[64];
  114.     FILE   *fp;
  115.  
  116.     while ((i = getopt (argc, argv, "hv")) != -1)
  117.     {
  118.         switch (i)
  119.         {
  120.         case 'v':
  121.             version ();
  122.             break;
  123.         case 'h':
  124.         default:
  125.             usage ();
  126.         }
  127.     }
  128.  
  129.     /* ensure that the configuration directory exists */
  130. /*    strcpy (path, SHAREDIR);
  131.     puts ("Checking for OpenNap configuration directory...");
  132.     if (access (path, F_OK))
  133.     {
  134.         if (errno != ENOENT)
  135.             perror (path); */
  136.         do
  137.         {
  138.             prompt ("Where should I install OpenNap", "", path,
  139.                     sizeof (path));
  140.             if (MKDIR (path, S_IRWXU))
  141.                 perror (path);
  142.         }
  143.         while (access (path, F_OK));
  144.         printf ("Created %s\n", path);
  145. /*    }*/
  146.     /* directory is created at this point */
  147.  
  148.     /* check to see if the users file exists */
  149.     puts ("Checking for OpenNap user database...");
  150.     snprintf (buf, sizeof (buf), "%s%cusers", path, DIRSEP);
  151.     if (access (buf, F_OK))
  152.     {
  153.         if (errno != ENOENT)
  154.         {
  155.             perror (buf);
  156.             exit (1);
  157.         }
  158.         /* prompt the user for the elite login */
  159.         prompt ("Enter nickname for server owner (elite)", 0, nick,
  160.                 sizeof (nick));
  161.         prompt ("Enter password for nickname", 0, pass, sizeof (pass));
  162.         prompt ("Enter email address", "email@here.com", email,
  163.                 sizeof (email));
  164.         /* write out the file */
  165.         fd = open (buf, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  166.         if (fd < 0)
  167.         {
  168.             perror ("open");
  169.             exit (1);
  170.         }
  171.         fp = fdopen (fd, "w");
  172.         if (!fp)
  173.         {
  174.             perror (buf);
  175.             exit (1);
  176.         }
  177.         fprintf (fp, "%s %s %s Elite 0 0", nick, pass, email);
  178.         if (fclose (fp))
  179.         {
  180.             perror ("fclose");
  181.             exit (1);
  182.         }
  183.         printf ("Created %s\n", buf);
  184.     }
  185.     /* if opennap is installed other than in the default location, set
  186.        up a config file with the proper config_dir variable */
  187. /*    if (strcmp (path, SHAREDIR) != 0)
  188.     {*/
  189.         snprintf (buf, sizeof (buf), "%s%cconfig", path, DIRSEP);
  190.         if (access (buf, F_OK))
  191.         {
  192.             if (errno != ENOENT)
  193.             {
  194.                 perror (buf);
  195.                 exit (1);
  196.             }
  197.             fd = open (buf, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  198.             if (fd < 0)
  199.             {
  200.                 perror ("open");
  201.                 exit (1);
  202.             }
  203.             fp = fdopen (fd, "w");
  204.             if (!fp)
  205.             {
  206.                 perror ("fdopen");
  207.                 exit (1);
  208.             }
  209.             fprintf (fp, "# auto generated by %s setup%s %s\n\n", PACKAGE,
  210.                      EXT, VERSION);
  211. /*            fprintf (fp, "# package was configured to install in %s\n",
  212.                      SHAREDIR);*/
  213.             fprintf(fp, "# SHAREDIR not supported in this version\n");
  214.             fprintf (fp, "# but OpenNap is installed here:\n");
  215.             fprintf (fp, "config_dir %s\n", path);
  216.             fclose (fp);
  217.             printf ("Created %s\n", buf);
  218.         }
  219.  
  220. #if WIN32
  221.         /* create a batch file to easily launch the server */
  222.         snprintf (buf, sizeof (buf), "%s\\launch.bat", path);
  223.         fp = fopen (buf, "w");
  224.         if (!fp)
  225.         {
  226.             perror (buf);
  227.             exit (1);
  228.         }
  229.         fprintf (fp, "%s\\opennap.exe -c %s\\config", path, path);
  230.         fclose (fp);
  231.         printf ("Created %s\n", buf);
  232.         printf ("You can start the server by running %s\\launch.bat\n", path);
  233. #endif /* WIN32 */
  234. /*    }
  235.     else
  236.         puts
  237.             ("OpenNap is installed in default directory, no config file created"); */
  238.     puts ("Congratulations!  OpenNap is now installed");
  239.  
  240.     exit (0);
  241. }
  242.